package com.t2t.top.framework.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogSessionUtils {
    static Logger logger = LoggerFactory.getLogger(LogSessionUtils.class);

    //创建线程局部变量
    private static final ThreadLocal<LogBo> session = new ThreadLocal();

    public static LogBo get() {
        LogBo bo = session.get();
        if (bo == null) {
            return null;
        }
        bo.setLine(bo.getLine() + 1);
        return bo;
    }

    public static LogBo getUnCount() {
        LogBo bo = session.get();
        if (bo == null) {
            return null;
        }
        return bo;
    }

    public static LogBo getCurrentLogBo() {
        LogBo bo = session.get();
        if (bo == null) {
            return null;
        }
        return bo;
    }

    public static void remove() {
        session.remove();
    }

    public static boolean isNotBind() {
        LogBo bo = session.get();
        return (bo == null);
    }

    public static boolean isBind() {
        LogBo bo = session.get();
        return !isNotBind();
    }

    public static void bind(String interfaceId, String transId) {
        session.remove();
        if (transId != null) {
            LogBo bo = new LogBo(interfaceId, transId, 0);
            session.set(bo);
            if (logger.isDebugEnabled()) {
                logger.debug("设置流程ID:" + bo);
            }
        }
    }
}

